package com.myapplication.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myapplication.zhou.alldemo.R;

/**
 * Created by Administrator on 2016/5/20.
 */
public class MyDialog {

    public Dialog tipDialogByTextAndTimeAndImage(Context context, String msg, final int time, int img) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View v = layoutInflater.inflate(R.layout.dialog_text_time_image, null);
        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog);
        TextView textView_info = (TextView) v.findViewById(R.id.textView_info);
        textView_info.setText(msg);
        if (img == 0)
            imageView.setVisibility(View.GONE);
        else imageView.setImageResource(img);
        final Dialog dialog = new Dialog(context, R.style.selectorDialog);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        ));
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = (int) (display.getWidth()*0.63);
        layoutParams.height = (int) (display.getHeight()*0.21);
        layoutParams.alpha = 0.9f;
        dialog.getWindow().setAttributes(layoutParams);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(time);
                    dialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        return dialog;
    }
}
